package com.freesky.biz.activity.controller;

import com.freesky.biz.activity.domain.TAssignCoupon;
import com.freesky.biz.activity.domain.TContract;
import com.freesky.biz.activity.domain.TProject;
import com.freesky.biz.activity.service.IProjectService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/project")
public class ProjectController extends BaseController {

    @Autowired
    private IProjectService projectService;

    /**
     * 获取活动配置列表
     */
    @PreAuthorize("@ss.hasPermi('project:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(TProject entity) {
        startPage();
        List<TProject> list = projectService.findList(entity);
        return getDataTable(list);
    }

    @GetMapping("/summary")
    public AjaxResult summary(TProject entity) {
        return AjaxResult.success(projectService.selectSummaryData(entity));
    }

    /**
     * 根据ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:config:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable String configId) {
        TProject project = projectService.get(configId);
        return success(project);
    }

    /**
     * 新增项目管理
     */
    @PreAuthorize("@ss.hasPermi('project:config:add')")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TProject entity) {
        entity.setId(SeqUtil.getSeqId());
        entity.setCreateBy(String.valueOf(getUserId()));
        if(!projectService.checkProjectNameUnique(entity)){
            return error("新增项目'" + entity.getName() + "'失败，项目名称已存在");
        }
        return toAjax(projectService.insert(entity));
    }

    /**
     * 修改项目管理
     */
    @PreAuthorize("@ss.hasPermi('project:config:edit')")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TProject entity) {
        entity.setUpdateBy(String.valueOf(getUserId()));
        if(!projectService.checkProjectNameUnique(entity)){
            return error("修改项目'" + entity.getName() + "'失败，项目名称已存在");
        }
        return toAjax(projectService.update(entity));
    }


    /**
     * 删除项目管理
     */
    @PreAuthorize("@ss.hasPermi('project:config:remove')")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable String[] configIds) {
        for(String configId : configIds){
            int nCount = projectService.selectCouponCount(configId);
            if(nCount > 0) {
                return AjaxResult.error("请先删除关联的券号!");
            }
        }

        projectService.deleteByIds(configIds);
        return success();
    }

    /**
     * 获取券的配置
     */
    @PreAuthorize("@ss.hasPermi('project:coupon:list')")
    @GetMapping("/couponList")
    public TableDataInfo couponList(TAssignCoupon entity) {
        startPage();
        List<TAssignCoupon> list = projectService.selectCouponList(entity);
        return getDataTable(list);
    }

    /**
     * 未关联券的配置
     */
    @PreAuthorize("@ss.hasPermi('project:coupon:list')")
    @GetMapping("/unAllocatedList")
    public TableDataInfo unAllocatedList(TAssignCoupon entity) {
        startPage();
        entity.setProjectId("-1");
        List<TAssignCoupon> list = projectService.selectCouponList(entity);
        return getDataTable(list);
    }

    /**
     * 分配券
     */
    @PreAuthorize("@ss.hasPermi('project:coupon:assign')")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PostMapping("/assignCoupon")
    public AjaxResult assignCoupon(@RequestBody TAssignCoupon entity) {
        projectService.assignCoupon(entity);
        return success();
    }

    /**
     * 取消分配券
     */
    @PreAuthorize("@ss.hasPermi('project:coupon:assign')")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PostMapping("/unAssignCoupon")
    public AjaxResult unAssignCoupon(@RequestBody TAssignCoupon entity) {
        projectService.unAssignCoupon(entity);
        return success();
    }

}
